use std::io;
use std::cmp::Ordering;
use rand::Rng;


fn main() {
    println!("猜数字游戏");

    let secret_number = rand::thread_rng().gen_range(1..101);
    // println!("密密数字是：{}", secret_number);

    loop {
        println!("请输入你的猜测: ");

        let mut guess = String::new();

        io::stdin()
            .read_line(&mut guess)
            .expect("行读取失败");

        // let guess: u32 = guess.trim().parse().expect("请输入一个数字！");
        let guess: u32 = match guess.trim().parse() {
            Ok (num) => num,
            Err (_) => continue,
        };

        println!("你的猜想： {}", guess);

        match guess.cmp(&secret_number) {
            Ordering::Less=> println!("太小"),
            Ordering::Greater => println!("太大"),
            Ordering::Equal => {
                println!("你赢了！");
                break;
            },
        }
    }
}
